<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi as UserApi;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class IndexController extends AdminController {

    /**
     * 后台首页
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        if(UID){
            $userinfo=M('group')->alias('a')->join('app_admins'." b on a.id=b.gid",'left')->where(array('b.id'=>UID,'b.status'=>1))
                ->field('b.*,a.rules,a.name as group_name')->find();
            //$userinfo = M('admins')->where(array('uid'=>UID))->find();
            session('adminInfo',$userinfo);
            $this->meta_title = '管理首页';
            static $Auth    =   null;
            if (!$Auth) {
                $Auth       =   new \Think\Auth();
            }

            $group_info = M('group')->alias('a')
                ->join('app_admins'." b on a.id=b.gid",'left')
                ->where(array('b.id'=>UID,'b.status'=>1))
                ->field('a.rules,a.name')->find();
            $ids=$group_info['rules'];
            $res=M('node')->where(array('id'=>array('in',$ids),'status'=>1,'type'=>array('lt',2)))->order("sort desc")->select();
           /* if(UID == 1){
                $res = M('node')->where(array('type'=>array('lt',array(2)),'status'=>1))->order("sort asc")->select();
            }else{
                $res = $Auth->getMenuList(UID,array(0,1));
            }*/
            foreach($res as &$v){
//                $auth = preg_replace('/\?.*$/U','',$v['name']);
//                if ( in_array($auth,$name) && $intersect==$param ) {  //如果节点相符且url参数满足
//                    $list[] = $auth ;
//                }
                $v['name'] = "http://".$_SERVER['HTTP_HOST']."/index.php?s=/Admin/".$v['name'];
            }
            $tree = list_to_tree($res,'id');

            $this->assign('lists',$tree);
            $this->display();

        } else {
            D('Member')->logout();
            session('[destroy]');
            $this->redirect('Admin/Public/login');
        }
    }
    /*
     * 首页欢迎页面
     */
    public function welcome(){
        $admin	 =session('adminInfo');
        $group=$this->getInfo('*','group','id='.$admin['gpid']);
        if($admin['gpid']==0)
        {
            $where ='1=1';
        }
        elseif($group['work_status']==0)
        {
            $where ='1=1';
        }
        else
        {
            $where ='gpid='.$admin['gpid'];
        }
        if($admin['remark'] == '业务员')
        {
            $where .=' and admin_id='.$admin['id'];
        }

        switch($admin['gpid'])
        {
            case 0:
                $not_credit	=	M('user')
                    ->where($where.' and check_status=-4 and status!=0')
                    ->count();
                $not_apply		=	M('user')
                    ->where($where.' and check_status=-1 and status=1')
                    ->count();
                $not_check		=	M('user')
                    ->where($where.' and check_status<6 and check_status>=0 and status=1')
                    ->count();
                $not_mat		=	M('user')
                    ->alias('u')
                    ->join('app_bank b on u.loan_bank_id=b.id','left')
                    ->join('app_advance a on a.user_id=u.id','left')
                    ->where($where.' and (check_status=6 and u.status!=101 )or (check_status=6 and u.status!=101 and a.status=0)')
                    ->count();
                $not_pay		=	M('user')
                    ->alias('u')
                    ->join('app_advance m on u.id=m.user_id')
                    ->join('app_bank b on u.loan_bank_id=b.id','left')
                    ->where($where.' and m.status>=2 and m.pay_type=0 and u.status!=101')
                    ->count();
                $not_wait		=	M('user')->where($where.' and status=9')->count();
                $not_inside	=	M('user')
                    ->where($where.' and (status = 1 or status = 9) and check_status=0')
                    ->count();
                $not_region	=	M('user')->where($where.' and check_status=1')->count();
                $not_assistant	=	M('user')
                    ->where($where.' and check_status=2 and status = 1')
                    ->count();
                $not_first		=	M('user')
                    ->where($where.' and check_status=3 and status=1')
                    ->count();
                $not_admin		=	M('user')
                    ->where($where.' and check_status=4 and status=1')
                    ->count();
                $not_manage	=	M('user')
                    ->where($where.' and check_status=5 and status=1')
                    ->count();
                $wait_pay		=	M('user')
                    ->alias('u')
                    ->join('app_advance m on u.id=m.user_id')
                    ->where($where.' and m.status>=2 and m.pay_type=0 and u.status!=101')
                    ->count();
                $wait_bank_pay	=	M('advance')
                    ->where($where.' and status>=2 and pay_type=0')
                    ->count();
                $wait_check	=	M('user')
                    ->alias('u')
                    ->join('app_advance b on u.id=b.user_id','left')
                    ->join('app_data_check m on m.user_id=u.id','left')
                    ->where($where.' and b.status=4 and (m.status=2 or m.status=99) and (u.status!=101 or u.status=9) and u.check_status=10')
                    ->count();
                $wait_print	=	M('user')
                    ->where($where.' and check_status=11 and status!=-1')
                    ->count();

                $data			=array(
                    array('征信待审核',$not_credit,'Apply/credit_waitCheck'), //2018/4/28 Lii
                    array('申请待提交',$not_apply,'Apply/wait_submit'),
                    array('申请待审核',$not_check,'Apply/wait_check'), //2018/4/28 Lii
                    array('垫资待申请',$not_mat,'MatApply/index'), //2018/4/28 Lii
                    array('垫资待打款',$not_pay,'Check/payMoney_index'), //2018/4/28 Lii
                    array('申请待定中',$not_wait,'Apply/wait_status'), //2018/4/28 Lii
                    array('待内勤审核',$not_inside,'Inside/check'), //2018/4/28 Lii
                    array('待经理审核',$not_region,'Check/Region_manager'), //2018/4/28 Lii
                    array('待审核助理审核',$not_assistant,'Check/assistant'), //2018/4/28 Lii
                    array('待审核专员审核',$not_first,'Check/frist'), //2018/4/28 Lii
                    array('待审核主管审核',$not_admin,'Check/admin'), //2018/4/28 Lii
                    array('待审核经理审核',$not_manage,'Check/manager'), //2018/4/28 Lii
                    array('待打款',$wait_pay,'Check/payMoney_index'), //2018/4/28 Lii
                    array('待银行放贷',$wait_bank_pay,'Check/payMoney_index'),
                    array('待抄单整理',$wait_check,'ChaoDan/Confirm'), //2018/4/28 Lii
                    array('待抄单',$wait_print,'ChaoDan/Prints'), //2018/4/28 Lii
                );
                break;

            case 31:
                $not_assistant	=M('user')
                    ->where($where.' and check_status=2 and status = 1')
                    ->count();
                $not_first		=	M('user')
                    ->where($where.' and check_status=3 and status=1')
                    ->count();
                $not_admin		=	M('user')
                    ->where($where.' and check_status=4 and status=1')
                    ->count();
                $not_manage		=	M('user')
                    ->where($where.' and check_status=5 and status=1')
                    ->count();
                $data			=array(
                    array('待审核助理审核',$not_assistant,'Check/assistant'),  //2018/4/28 Lii
                    array('待审核专员审核',$not_first,'Check/frist'),  //2018/4/28 Lii
                    array('待审核主管审核',$not_admin,'Check/admin'),  //2018/4/28 Lii
                    array('待审核经理审核',$not_manage,'Check/manager'),  //2018/4/28 Lii
                );
                break;

            case 30:
                $not_credit		=	M('user')
                    ->where($where.' and check_status=-4 and status!=0')
                    ->count(); //2018/4/28 Lii

                $data		= array(
                    array('征信待查询',$not_credit,'Find/credit_list'),
                );
                break;

            default:
                $not_credit		=	M('user')
                    ->where($where.' and check_status=-4 and status!=0')
                    ->count(); //2018/4/28 Lii
                $not_apply		=	M('user')
                    ->where($where.' and check_status=-1 and status=1')
                    ->count();
                $not_check		=	M('user')
                    ->where($where.' and check_status<6 and check_status>=0 and status=1')
                    ->count();
                $not_mat		=	M('user')
                    ->alias('u')
                    ->join('app_bank b on u.loan_bank_id=b.id','left')
                    ->join('app_advance a on a.user_id=u.id','left')
                    ->where($where.' and (check_status=6 and u.status!=101 )or (check_status=6 and u.status!=101 and a.status=0)')
                    ->count();
                $not_pay		=	M('user')
                    ->alias('u')
                    ->join('app_advance m on u.id=m.user_id')
                    ->join('app_bank b on u.loan_bank_id=b.id','left')
                    ->where($where.' and m.status>=2 and m.pay_type=0 and u.status!=101')
                    ->count();
                $not_wait		=	M('user')->where($where.' and status=9')->count();
                $not_inside	    =	M('user')
                    ->where($where.' and (status = 1 or status = 9) and check_status=0')
                    ->count();
                $not_region	    =	M('user')
                    ->where($where.' and check_status=1 and status=1')
                    ->count();

                $data			=array(
                    array('征信待审核',$not_credit,'Apply/credit_waitCheck'),  //2018/4/28 Lii
                    array('申请待提交',$not_apply,'Apply/wait_submit'),
                    array('申请待审核',$not_check,'Apply/wait_check'), //2018/4/28 Lii
                    array('垫资待申请',$not_mat,'MatApply/index'), //2018/4/28 Lii
                    array('垫资待打款',$not_pay,'Check/payMoney_index'), //2018/4/28 Lii
                    array('申请待定中',$not_wait,'Apply/wait_status'), //2018/4/28 Lii
                    array('待内勤审核',$not_inside,'Inside/check'),  //2018/4/28 Lii
                    array('待经理审核',$not_region,'Check/Region_manager'),
                );
        }
        $this->assign('data',$data);
        $this->display();
    }
    /*
     * 访问未授权的页面
     */
    public function notAuth(){
        $this->display();
    }

}
